//
//  HLUserDetailBottomView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/18.
//

#import "HLUserDetailBottomView.h"
#import "HLGiftContainerView.h"

@interface HLUserDetailBottomView ()

@property (nonatomic, strong) QMUIButton *giftButton;
@property (nonatomic, strong) QMUIButton *greetButton;
@property (nonatomic, strong) QMUIButton *chatButton;
@property (nonatomic, strong) QMUIButton *videoButton;

@end

@implementation HLUserDetailBottomView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    self.backgroundColor = UIColorWhite;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(64 + SafeAreaInsetsConstantForDeviceWithNotch.bottom);
    }];
    
    self.giftButton = [[QMUIButton alloc] init];
    [_giftButton setImage:UIImageMake(@"user_detail_gift") forState:UIControlStateNormal];
    [_giftButton addTarget:self action:@selector(giftAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_giftButton];
    [_giftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(12);
        make.size.mas_equalTo(40);
    }];
    
    self.greetButton = [[QMUIButton alloc] init];
    [_greetButton setImage:UIImageMake(@"home_cell_greet") forState:UIControlStateNormal];
    [_greetButton addTarget:self action:@selector(greetAction) forControlEvents:UIControlEventTouchUpInside];
    [_greetButton setTitle:@"搭讪" forState:UIControlStateNormal];
    [_greetButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    _greetButton.cornerRadius = 20;
    _greetButton.layer.borderWidth = 1.5;
    _greetButton.layer.borderColor = HLTextColor1.CGColor;
    _greetButton.titleLabel.font = UIFontMediumMake(15);
    _greetButton.imagePosition = QMUIButtonImagePositionLeft;
    _greetButton.spacingBetweenImageAndTitle = 2;
    [self addSubview:_greetButton];
    _greetButton.hidden = YES;
    [_greetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_giftButton.mas_right).offset(16);
        make.centerY.equalTo(_giftButton).offset(3);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    self.chatButton = [[QMUIButton alloc] init];
    [_chatButton setImage:UIImageMake(@"home_cell_chat") forState:UIControlStateNormal];
    [_chatButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    [_chatButton setTitle:@"私信" forState:UIControlStateNormal];
    [_chatButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
    _chatButton.cornerRadius = 20;
    _chatButton.layer.borderWidth = 1.5;
    _chatButton.layer.borderColor = HLTextColor3.CGColor;
    _chatButton.titleLabel.font = UIFontMediumMake(15);
    _chatButton.imagePosition = QMUIButtonImagePositionLeft;
    _chatButton.spacingBetweenImageAndTitle = 2;
    [self addSubview:_chatButton];
    [_chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.centerY.left.equalTo(_greetButton);
    }];
    
    self.videoButton = [[QMUIButton alloc] init];
    [_videoButton setImage:UIImageMake(@"home_video_icon") forState:UIControlStateNormal];
    [_videoButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_videoButton.titleLabel setFont:UIFontBoldMake(14)];
    [_videoButton setBackgroundColor:HLMainColor];
    [_videoButton addTarget:self action:@selector(videoAction) forControlEvents:UIControlEventTouchUpInside];
    _videoButton.cornerRadius = 20;
    _videoButton.imagePosition = QMUIButtonImagePositionLeft;
    _videoButton.spacingBetweenImageAndTitle = 4;
    [self addSubview:_videoButton];
    [_videoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.left.equalTo(self.mas_centerX).offset(0);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(_giftButton).offset(3);
    }];
    
    self.editButton = [[QMUIButton alloc] init];
    [_editButton setImage:UIImageMake(@"user_detail_edit") forState:UIControlStateNormal];
    [_editButton setTitle:@"去编辑" forState:UIControlStateNormal];
    [_editButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_editButton.titleLabel setFont:UIFontMediumMake(14)];
    [_editButton setBackgroundColor:HLMainColor];
    _editButton.cornerRadius = 22;
    _editButton.imagePosition = QMUIButtonImagePositionLeft;
    _editButton.spacingBetweenImageAndTitle = 4;
    [self addSubview:_editButton];
    [_editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(160, 44));
    }];
}

- (void)setModel:(HLUserInfoModel *)model
{
    _model = model;
    [self updateButton];
    if ([model.videoStatus boolValue]){
        [self animateButton];
    }
}

- (void)animateButton
{
    [self.videoButton.imageView.layer removeAllAnimations];
    CABasicAnimation*posAni = [CABasicAnimation animationWithKeyPath:@"position"];
    posAni.toValue=[NSValue valueWithCGPoint:self.videoButton.imageView.center];
    CABasicAnimation*opcAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opcAni.toValue=[NSNumber numberWithFloat:0.8];
    CABasicAnimation*bodAni = [CABasicAnimation animationWithKeyPath:@"bounds"];
    bodAni.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 20, 20)];
    CAAnimationGroup *groupAni = [CAAnimationGroup animation];
    groupAni.animations = @[posAni,opcAni,bodAni];
    groupAni.duration = 0.6;
    groupAni.fillMode = kCAFillModeForwards;
    groupAni.removedOnCompletion = NO;
    groupAni.autoreverses = YES;
    groupAni.repeatCount = MAXFLOAT;
    groupAni.timingFunction = [CAMediaTimingFunction                 functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.videoButton.imageView.layer addAnimation:groupAni forKey:@"groupAni"];
}

- (void)updateButton
{
    _giftButton.hidden = YES;
    _greetButton.hidden = YES;
    _chatButton.hidden = YES;
    _videoButton.hidden = YES;
    _editButton.hidden = YES;
    
    if ([HLRunTime shareInstance].isFood){
        if (![_model.userId isEqualToString:[HLLoginModel dataWithKeychain].userId]){
            [_editButton setImage:UIImageMake(@"user_detail_chat") forState:UIControlStateNormal];
            [_editButton setTitle:@"讨教心得" forState:UIControlStateNormal];
            [_editButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
            _editButton.hidden = NO;
        }
        return;
    }
    
    if ([_model.userId isEqualToString:[HLLoginModel dataWithKeychain].userId]){
        _editButton.hidden = NO;
    } else {
        
        _giftButton.hidden = NO;
        _greetButton.hidden = NO;
        _chatButton.hidden = NO;
        _videoButton.hidden = NO;
        
        _greetButton.enabled = YES;
        _chatButton.enabled = YES;
        _greetButton.hidden = [_model.chatUp boolValue];
        _chatButton.hidden = ![_model.chatUp boolValue];
        [_videoButton setTitle:[NSString stringWithFormat:@"%ld金币/分钟", [_model.videoPrice integerValue]] forState:UIControlStateNormal];
    }
}

- (void)chatAction
{
    [HLUIHelper chatWithUserId:_model.userId controller:self.qmui_viewController];
}

- (void)videoAction
{
    [HLUIHelper callWithUser:self.model];
}

- (void)greetAction
{
    _greetButton.enabled = NO;
    MJWeakSelf
    [HLUIHelper greetWithParam:@{@"toMemberId":_model.userId} callBack:^(BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess){
            weakSelf.model.chatUp = @"1";
        } else {
            if (message.length > 0) {
                [QMUITips showWithText:message];
            }
        }
        
        [weakSelf updateButton];
    }];
}

- (void)giftAction
{
    [HLGiftContainerView showWithPlaceType:HLGiftPlaceTypeUser user:_model callback:^(id obj) {
        [QMUITips showWithText:@"赠送礼物成功"];
    }];
}

@end
